package Views;

import Controllers.MachineCTR;
import Models.Machine;
import Views.TableModels.MachineTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Morten
 */
public class MachinePicker extends javax.swing.JFrame implements ListSelectionListener
{
	private Machine chosenMachine;
	private MachineCTR controller = new MachineCTR();

    public MachinePicker()
	{
        initComponents();
		machineTable.getSelectionModel().addListSelectionListener(this);
		machineTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(machineTable.getSelectedRowCount() == 1)
					{
						chooseButtonActionPerformed(null);
					}
				}
			}
		 });
    }

	public Machine getChosenMachine()
	{
		return this.chosenMachine;
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        availableBG = new javax.swing.ButtonGroup();
        nameTF = new javax.swing.JTextField();
        availableHomeRB = new javax.swing.JRadioButton();
        availableOutRB = new javax.swing.JRadioButton();
        searchButton = new javax.swing.JButton();
        jScrollPane = new javax.swing.JScrollPane();
        machineTable = new javax.swing.JTable();
        chooseButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Maskinevælger");

        availableBG.add(availableHomeRB);
        availableHomeRB.setText("Hjemme");

        availableBG.add(availableOutRB);
        availableOutRB.setText("Udlejet");

        searchButton.setText("Søg");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        machineTable.setModel(new MachineTableModel());
        jScrollPane.setViewportView(machineTable);

        chooseButton.setText("Vælg");
        chooseButton.setEnabled(false);
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Annuller");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(chooseButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(nameTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(availableHomeRB)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(availableOutRB)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(searchButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 67, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(jScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(31, 31, 31)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(searchButton)
                    .add(availableHomeRB)
                    .add(availableOutRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(chooseButton)
                    .add(cancelButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void searchButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_searchButtonActionPerformed
	{//GEN-HEADEREND:event_searchButtonActionPerformed
		boolean available;
		if(availableHomeRB.isSelected())
		{
			available = true;
		}
		else
		{
			available = false;
		}
		this.machineTable.setModel(new MachineTableModel(this.controller.findMachine(nameTF.getText(), available)));
}//GEN-LAST:event_searchButtonActionPerformed

	private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_chooseButtonActionPerformed
	{//GEN-HEADEREND:event_chooseButtonActionPerformed
		int chosenId = Integer.parseInt(machineTable.getValueAt(machineTable.getSelectedRow(), 0).toString());
		this.chosenMachine = this.controller.getMachine(chosenId);
		if(chosenMachine.isAvailable())
		{
			this.doClose();
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Det er ikke muligt at udleje '"+this.chosenMachine.getName()+"', da den allerede er udlejet.", "Umulig udlejning", JOptionPane.ERROR_MESSAGE);
			this.chosenMachine = null;
		}
}//GEN-LAST:event_chooseButtonActionPerformed

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
	{//GEN-HEADEREND:event_cancelButtonActionPerformed
		this.doClose();
}//GEN-LAST:event_cancelButtonActionPerformed

	public void valueChanged(ListSelectionEvent e)
	{
		if(machineTable.getSelectedRowCount() == 1)
		{
			chooseButton.setEnabled(true);
		}
		else
		{
			chooseButton.setEnabled(false);
		}
	}

	public void doClose()
	{
        WindowEvent evt = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        processWindowEvent(evt);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup availableBG;
    private javax.swing.JRadioButton availableHomeRB;
    private javax.swing.JRadioButton availableOutRB;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTable machineTable;
    private javax.swing.JTextField nameTF;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables
}